package com.erp.erp_ui.warehouse.controller;

import com.alibaba.fastjson.JSONObject;
import com.erp.erp_entitys.User.UserEntity;
import com.erp.erp_entitys.business.req.CarryDetailStatisticsPaymentReq;
import com.erp.erp_entitys.warehouse.req.RentalFeePageListReq;
import com.erp.erp_ui.base.Base;
import com.erp.erp_ui.money.req.OperationReq;
import com.erp.erp_ui.warehouse.req.AccountAgeReq;
import com.erp.erp_ui.warehouse.req.AddRentalFeeReq;
import com.erp.erp_ui.warehouse.req.SelectStockReq;
import com.erp.erp_ui.warehouse.req.StockProductReq;
import com.erp.erp_ui.warehouse.service.StockService;
import com.erp.erp_ui.warehouse.vo.*;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


/**
 * @ClassName : StockController
 * @Description : 库存控制层
 * @Author : lst
 * @Date: 2020-07-15 08:28
 */
@Controller
@RequestMapping(value = "stock")
public class StockController extends Base {

    /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(StockController.class);

    /**
     * 库存业务逻辑接口
     */
    private final StockService stockService;

    @Autowired
    public StockController(StockService stockService) {
        this.stockService = stockService;
    }

    /**
     * 获取库存列表
     *
     * @param req
     * @return
     */
    @GetMapping("/listByPage")
    @ResponseBody
    public R list(SelectStockReq req) {
        LOGGER.info("库存-获取库存列表 req=" + req.toString());
        try {
            PageInfo<StockVo> stockList = stockService.getStockList(req);
            return R.build(0, "", stockList.getTotal(), stockList.getList());
        } catch (Exception e) {
            LOGGER.warn("库存-获取库存列表异常" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }

    }

    /**
     * 获取库存明细
     * @param id 库存id
     * @return 库存明细集合
     */
    @GetMapping("/getStockDetailByPage")
    @ResponseBody
    public R getStockDetailById(Integer id){
        try {
            PageInfo<StockDetailVo> list = stockService.getStockDetailById(id);
            return R.build(0, "", list.getTotal(), list.getList());
        }catch (Exception e){
            LOGGER.warn("库存-获取库存明细列表异常" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取库存账龄
     * @param req
     * @return
     */
    @GetMapping("/getStockGoodsAgeByPage")
    @ResponseBody
    public R getStockGoodsAge(AccountAgeReq req) {
        LOGGER.info("库存-获取库存账龄 req=" + req.toString());
        try {
            PageInfo<StockAccountAgeVo> ageVos = stockService.getStockGoodsAge(req);
            return R.build(0, "", ageVos.getTotal(), ageVos.getList());
        }catch (Exception e){
            LOGGER.warn("库存-获取库存账龄异常" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取库存产品
     * @param req 获取库存产品入参实体
     * @return 库存集合
     */
    @GetMapping("/getStockProductByPage")
    @ResponseBody
    public R getStockProductByPage(StockProductReq req){
        LOGGER.info("库存-获取库存产品集合 req=" + req.toString());
        try {
            PageInfo<StockProductVo> stockVos = stockService.getStockProductByPage(req);
            return R.build(0, "", stockVos.getTotal(), stockVos.getList());
        }catch (Exception e){
            LOGGER.warn("库存-获取库存产品集合异常" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 海外仓库租金费-新增
     * @param req 新增信息对象
     * @return 新增结果
     */
    @PostMapping("/add/rentalFee")
    @ResponseBody
    public R addRentalFeeInfo(@RequestBody AddRentalFeeReq req){
        try {
            LOGGER.info("仓库管理-海外仓库租金费-新增 req=" + req.toString());
            UserEntity user = (UserEntity)this.getBean("user");
            String name = user.getName();
            String code = user.getCode();
            return stockService.addRentalFeeInfo(req, name, code);
        }catch (Exception e){
            LOGGER.info("仓库管理-海外仓库租金费-新增异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 首页-分页列表-海外仓库租金费
     *
     * @param req 查询参数
     * @return 海外仓库租金费
     */
    @GetMapping("/getRentalFeeInfoByPage")
    @ResponseBody
    public R getRentalFeeInfoByPage(RentalFeePageListReq req) {
        try {
            LOGGER.info("首页-海外仓库租金费-分页列表查询 req=" + req.toString());
            PageInfo<RentalFeePageListVo> pageInfo = stockService.getRentalFeeInfoByPage(req);
            return R.build(0, "", pageInfo.getTotal(), pageInfo.getList());
        } catch (Exception e) {
            LOGGER.info("首页-海外仓库租金费-分页列表查询异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 数据操作接口
     * 1.删除 2.执行 3.终止
     * @param req 操作类型
     * @return 操作结果
     */
    @PostMapping("/rental/operation")
    @ResponseBody
    public R operation(@RequestBody OperationReq req) {
        try {
            LOGGER.info("仓库管理-海外仓库租金费-数据操作接口 req=" + req);
            return stockService.operation(req);
        } catch (Exception e) {
            LOGGER.info("仓库管理-海外仓库租金费-数据操作接口异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 库存导出
     *
     * @param req 查询参数
     * @return
     */
    @GetMapping(value = "export/stockExport")
    @ResponseBody
    public R getStockExport(SelectStockReq req) {
        try {
            LOGGER.info("仓库管理-库存导出 req=" + req.toString());
            JSONObject obj = stockService.getStockExport(req);
            return R.build(obj);
        } catch (Exception e) {
            LOGGER.info("仓库管理-库存导出异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

}
